package com.zs.netlibrary.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.zs.netlibrary.NetApplication;

import java.util.HashSet;

/**
 * Created by sheng on 2017/8/18.
 */

public class CookiePreferenceUtils {
    private volatile static CookiePreferenceUtils instance;
    private final static String TAG = "CookiePreferenceUtils";
    private final static String COOKIE = "cookie";
    private SharedPreferences sharedPreferences;
    private CookiePreferenceUtils(){
        sharedPreferences = NetApplication.getInstance().getNetContext().getSharedPreferences(COOKIE, Context.MODE_PRIVATE);
    }

    public static CookiePreferenceUtils getInstance(){
        if(instance == null){
            synchronized (CookiePreferenceUtils.class){
                if(instance == null){
                    instance = new CookiePreferenceUtils();
                }
            }
        }
        return instance;
    }

    public void setCookie(String key, String cookie) {
        sharedPreferences.edit().putString(key, cookie).commit();
        Log.v(TAG, "setCookie and cookie is " + cookie);
    }

    public String getCookie(String key) {
        String mCookie = sharedPreferences.getString(key, "");
        Log.v(TAG, "getCookie, and Cookie is " + mCookie);
        return mCookie;
    }

    public void setCookie(String key, HashSet<String> cookie) {
        sharedPreferences.edit().putStringSet(key, cookie).commit();
        Log.v(TAG, "setCookie and cookie is " + cookie);
    }

    public HashSet<String> getCookieSet(String key) {
        HashSet<String> mCookie = (HashSet<String>) sharedPreferences.getStringSet(key, new HashSet<String>());
        Log.v(TAG, "getCookie, and Cookie is " + mCookie);
        return mCookie;
    }


}
